/**********************************************************/
/* Goblin Defense - A tower defense game for Wii Homebrew */
/* Copyright (C) 2009 Ricardo Bueno Cordeiro              */
/* Licensed under de GNU General Public License v2.0      */
/*   See license_gplv2.txt for detalis                    */
/**********************************************************/

#include <gccore.h>

#include "tool_weapon.h"
#include "draw.h"

#define NUM_TOOL_PER_LINE 3
#define TOOL_OFFSET_X ((((f32)MAP_SIZE) * 4.0 / 3.0 - ((f32)MAP_SIZE)) / ((f32)NUM_TOOL_PER_LINE))
#define TOOL_OFFSET_Y TOOL_OFFSET_X
#define TOOL_START_X (((f32)MAP_SIZE) / 2.0 + (TOOL_OFFSET_X / 2.0))
#define TOOL_START_Y (((f32)MAP_SIZE) / 2.0 + (TOOL_OFFSET_Y / 2.0))

f32 ToolWeapon::get_x() const {
	return TOOL_START_X + TOOL_OFFSET_X * (this->pos % NUM_TOOL_PER_LINE);
}

f32 ToolWeapon::get_y() const {
	return TOOL_START_Y + TOOL_OFFSET_Y * (this->pos / NUM_TOOL_PER_LINE);
}

void ToolWeapon::draw_tool() const {
	Sprite::draw_sprite(this->get_x(), this->get_y(), TOOL_OFFSET_X, TOOL_OFFSET_Y, this->get_sprite());
}

void ToolWeapon::mark_selected(u8 cr, u8 cg, u8 cb, u8 ca) const {
	f32 x = this->get_x();
	f32 y = this->get_y();
	f32 w = TOOL_OFFSET_X / 2.0;
	f32 h = TOOL_OFFSET_Y / 2.0;

	GX_Begin(GX_QUADS, GX_VTXFMT0, 4);

	GX_Position3f32(x - w, y - h, 0.0);
	GX_Color4u8(cr, cg, cb, ca);

	GX_Position3f32(x + w, y - h, 0.0);
	GX_Color4u8(cr, cg, cb, ca);

	GX_Position3f32(x + w, y + h, 0.0);
	GX_Color4u8(cr, cg, cb, ca);

	GX_Position3f32(x - w, y + h, 0.0);
	GX_Color4u8(cr, cg, cb, ca);

	GX_End();
}

int ToolWeapon::inside(f32 cursor_x, f32 cursor_y) const {
	f32 x = this->get_x();
	f32 y = this->get_y();
	f32 w = TOOL_OFFSET_X / 2.0;
	f32 h = TOOL_OFFSET_Y / 2.0;

	if ((cursor_x < (x - w)) || (cursor_x >= (x + w)) || (cursor_y < (y - h)) || (cursor_y >= (y + h)))
		return 0;
	return 1;
}
